package de.lulusoft.anothertorrent.core.rtorrent;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

public class Tracker {
	
	public static final String PROP_CHANGED = "trackerPropChanged";
	
	private String group;
	private String id;
	private Long min_interval;
	private Long normal_interval;
	private Long scrape_complete;
	private Long scrape_downloaded;
	private Long scrape_time_last;
	private Long type;
	private String url;
	private Long enabled;
	private Long open;
	private Torrent torrent;
	private Long index;
	
	private PropertyChangeSupport propertyChangeSupport = null;
	
	public Tracker(String group, String id, Long min_interval,
			Long normal_interval, Long scrape_complete, Long scrape_downloaded,
			Long scrape_time_last, Long type, String url, Long enabled,
			Long open, Torrent torrent, Long index) {
		super();
		this.group = group;
		this.id = id;
		this.min_interval = min_interval;
		this.normal_interval = normal_interval;
		this.scrape_complete = scrape_complete;
		this.scrape_downloaded = scrape_downloaded;
		this.scrape_time_last = scrape_time_last;
		this.type = type;
		this.url = url;
		this.enabled = enabled;
		this.open = open;
		this.torrent = torrent;
		this.index = index;
		
		propertyChangeSupport = new PropertyChangeSupport(this);
	}
	
	/**
	 * Updates this tracker with the new values.
	 * does not notify propertychangelisteners, so update can be called from a non gui thread.
	 * Returns 0 if no change occurred, 1 if a change occurred.
	 * @return
	 */
	public int update(String group, String id, Long min_interval,
			Long normal_interval, Long scrape_complete, Long scrape_downloaded,
			Long scrape_time_last, Long type, String url, Long enabled,
			Long open) {
		boolean changed = false;
		if (!this.group.equals(group)) { this.group = group; changed = true; }
		if (!this.id.equals(id)) { this.id = id; changed = true; }
		if (!this.min_interval.equals(min_interval)) { this.min_interval = min_interval; changed = true; }
		if (!this.normal_interval.equals(normal_interval)) { this.normal_interval = normal_interval; changed = true; }
		if (!this.scrape_complete.equals(scrape_complete)) { this.scrape_complete = scrape_complete; changed = true; }
		if (!this.scrape_downloaded.equals(scrape_downloaded)) { this.scrape_downloaded = scrape_downloaded; changed = true; }
		if (!this.scrape_time_last.equals(scrape_time_last)) { this.scrape_time_last = scrape_time_last; changed = true; }
		if (!this.type.equals(type)) { this.type = type; changed = true; }
		if (!this.url.equals(url)) { this.url = url; changed = true; }
		if (!this.enabled.equals(enabled)) { this.enabled = enabled; changed = true; }
		if (!this.open.equals(open)) { this.open = open; changed = true; }
		
		if (changed)
		{	
			return 1;
		}
		else
			return 0;
	}

	public String getGroup() {
		return group;
	}

	public void setGroup(String group) {
		this.group = group;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.group);
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.id);
	}

	public Long getMin_interval() {
		return min_interval;
	}

	public void setMin_interval(Long min_interval) {
		this.min_interval = min_interval;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.min_interval);
	}

	public Long getNormal_interval() {
		return normal_interval;
	}

	public void setNormal_interval(Long normal_interval) {
		this.normal_interval = normal_interval;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.normal_interval);
	}

	public Long getScrape_complete() {
		return scrape_complete;
	}

	public void setScrape_complete(Long scrape_complete) {
		this.scrape_complete = scrape_complete;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.scrape_complete);
	}

	public Long getScrape_downloaded() {
		return scrape_downloaded;
	}

	public void setScrape_downloaded(Long scrape_downloaded) {
		this.scrape_downloaded = scrape_downloaded;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.scrape_downloaded);
	}

	public Long getScrape_time_last() {
		return scrape_time_last;
	}

	public void setScrape_time_last(Long scrape_time_last) {
		this.scrape_time_last = scrape_time_last;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.scrape_time_last);
	}

	public Long getType() {
		return type;
	}

	public void setType(Long type) {
		this.type = type;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.type);
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.url);
	}

	public Long getEnabled() {
		return enabled;
	}

	public void setEnabled(Long enabled) {
		this.enabled = enabled;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.enabled);
	}

	public Long getOpen() {
		return open;
	}

	public void setOpen(Long open) {
		this.open = open;
		this.propertyChangeSupport.firePropertyChange(PROP_CHANGED, null, this.open);
	}

	public Torrent getTorrent() {
		return torrent;
	}
	
	public Long getIndex() {
		return index;
	}

	public void addPropertyChangeListener(PropertyChangeListener listener)
	{
		propertyChangeSupport.addPropertyChangeListener(PROP_CHANGED, listener);
	}
	
	public void removePropertyChangeListener(PropertyChangeListener listener)
	{
		propertyChangeSupport.removePropertyChangeListener(PROP_CHANGED, listener);
	}
	
	
}
